import axios from "axios";

axios.interceptors.request.use(
    config => {
        let token = localStorage.getItem("token") || ""
        if (token) {
            config.headers['token'] = token;
        }
        return config

    }, error => {
        return Promise.reject(error)
    }
)

export const http = function (url, params, method = "get", type = "Json") {
    if (method == 'get') {
        let t = new Date()
        if (params) {
            params.t = t
        } else {
            params = { t }
        }
    }
    return new Promise((resolve, reject) => {
        axios({
            url,
            method,
            type,
            data: params,
            params: params,
        })
            .then(result => {
                resolve(result.data)
            }).catch(error => {
                reject(error)
            })
    })
}

export const to = promise => {
    return promise.then(
        function (res) {
            return [null, res]
        }
    ).catch(
        function (err) {
            return [err]
        }
    )
}